package com.game.majiang.vo;

import java.sql.SQLException;
import java.sql.Types;
import java.util.Calendar;

import javax.sql.rowset.CachedRowSet;

import com.game.server.db.Parameter;
import com.game.server.db.ParameterType;
import com.game.server.db.SqlHelper;
import com.game.server.db.SqlHelperwrite;

public class TableMessageVo {
	
	public static int insert(int uid,int difen,int tableid,int alldairu,int roomtype,int gamechang,int caotype,int dairu,int shenuid) {
		String cmdText = "insert into tablemessage(uid,difen,tableid,alldairu,roomtype,gamechang,caotype,dairu,shenuid) values(?,?,?,?,?,?,?,?,?)";
		Parameter p1 = new Parameter(Types.INTEGER, uid, ParameterType.Input);
		Parameter p2 = new Parameter(Types.INTEGER, difen, ParameterType.Input);
		Parameter p3 = new Parameter(Types.INTEGER, tableid, ParameterType.Input);
		Parameter p4 = new Parameter(Types.INTEGER, alldairu, ParameterType.Input);
		Parameter p5 = new Parameter(Types.INTEGER, roomtype, ParameterType.Input);
		Parameter p6 = new Parameter(Types.INTEGER, gamechang, ParameterType.Input);
		Parameter p7 = new Parameter(Types.INTEGER, caotype, ParameterType.Input);
		Parameter p8 = new Parameter(Types.INTEGER, dairu, ParameterType.Input);
		Parameter p9 = new Parameter(Types.INTEGER, shenuid, ParameterType.Input);
		int key=SqlHelperwrite.executerekey(cmdText,p1,p2,p3,p4,p5,p6,p7,p8,p9);
		return key;
	}
	
	
	public static void update(int id,int caozuo,int dairu,boolean f,int createid,int tongyiuid) {
		String sql = "UPDATE tablemessage SET caotype="+caozuo+",alldairu=alldairu+"+dairu+" where id="+id+"";
		SqlHelperwrite.execute(sql);
		if(caozuo==1) {
			String cmdText1 = "select uid,difen,tableid,alldairu,roomtype,gamechang,caotype,dairu from tablemessage where  id="+id+"";
			CachedRowSet rs =SqlHelperwrite.getResultSet(cmdText1);
			try {
				if (rs.next()) 
				{
					if(rs.getInt(6)==2) {
						if(rs.getInt(1)!=tongyiuid) {
							String cmdText12 = "insert into tablemessage(uid,difen,tableid,alldairu,roomtype,gamechang,caotype,dairu,shenuid) values(?,?,?,?,?,?,?,?,?)";
							Parameter p1 = new Parameter(Types.INTEGER, tongyiuid, ParameterType.Input);
							Parameter p2 = new Parameter(Types.INTEGER, rs.getInt(2), ParameterType.Input);
							Parameter p3 = new Parameter(Types.INTEGER, rs.getInt(3), ParameterType.Input);
							Parameter p4 = new Parameter(Types.INTEGER, rs.getInt(4), ParameterType.Input);
							Parameter p5 = new Parameter(Types.INTEGER, rs.getInt(5), ParameterType.Input);
							Parameter p6 = new Parameter(Types.INTEGER, rs.getInt(6), ParameterType.Input);
							Parameter p7 = new Parameter(Types.INTEGER, rs.getInt(7), ParameterType.Input);
							Parameter p8 = new Parameter(Types.INTEGER, rs.getInt(8), ParameterType.Input);
							Parameter p9 = new Parameter(Types.INTEGER, rs.getInt(1), ParameterType.Input);
							SqlHelperwrite.execute(cmdText12,p1,p2,p3,p4,p5,p6,p7,p8,p9);
						}
						
						if(f) {
							String cmdText11 = "insert into tablemessage(uid,difen,tableid,alldairu,roomtype,gamechang,caotype,dairu,shenuid) values(?,?,?,?,?,?,?,?,?)";
							Parameter p11 = new Parameter(Types.INTEGER, createid, ParameterType.Input);
							Parameter p21 = new Parameter(Types.INTEGER, rs.getInt(2), ParameterType.Input);
							Parameter p31 = new Parameter(Types.INTEGER, rs.getInt(3), ParameterType.Input);
							Parameter p41 = new Parameter(Types.INTEGER, rs.getInt(4), ParameterType.Input);
							Parameter p51 = new Parameter(Types.INTEGER, rs.getInt(5), ParameterType.Input);
							Parameter p61 = new Parameter(Types.INTEGER, rs.getInt(6), ParameterType.Input);
							Parameter p71 = new Parameter(Types.INTEGER, rs.getInt(7), ParameterType.Input);
							Parameter p81 = new Parameter(Types.INTEGER, rs.getInt(8), ParameterType.Input);
							Parameter p91 = new Parameter(Types.INTEGER, rs.getInt(1), ParameterType.Input);
							SqlHelperwrite.execute(cmdText11,p11,p21,p31,p41,p51,p61,p71,p81,p91);
							
						
						}
					}else if (rs.getInt(6)==1&&rs.getInt(1)!=createid) {
						String cmdText12 = "insert into tablemessage(uid,difen,tableid,alldairu,roomtype,gamechang,caotype,dairu,shenuid) values(?,?,?,?,?,?,?,?,?)";
						Parameter p1 = new Parameter(Types.INTEGER, createid, ParameterType.Input);
						Parameter p2 = new Parameter(Types.INTEGER, rs.getInt(2), ParameterType.Input);
						Parameter p3 = new Parameter(Types.INTEGER, rs.getInt(3), ParameterType.Input);
						Parameter p4 = new Parameter(Types.INTEGER, rs.getInt(4), ParameterType.Input);
						Parameter p5 = new Parameter(Types.INTEGER, rs.getInt(5), ParameterType.Input);
						Parameter p6 = new Parameter(Types.INTEGER, rs.getInt(6), ParameterType.Input);
						Parameter p7 = new Parameter(Types.INTEGER, rs.getInt(7), ParameterType.Input);
						Parameter p8 = new Parameter(Types.INTEGER, rs.getInt(8), ParameterType.Input);
						Parameter p9 = new Parameter(Types.INTEGER, rs.getInt(1), ParameterType.Input);
						SqlHelperwrite.execute(cmdText12,p1,p2,p3,p4,p5,p6,p7,p8,p9);
					}
				}
			} catch (SQLException e) {
				e.printStackTrace();
				
			}
			
			
		}
	}
	
	public static int lookalltabledairu(int id) {
		int i=0;
		String cmdText = "select alldairu from tablemessage where  id="+id+"";
		CachedRowSet rs =SqlHelper.getResultSet(cmdText);
		try {
			if (rs.next()) 
			{
				i=rs.getInt(1);
			}
			
		} catch (SQLException e) {
			e.printStackTrace();
			
		}
		return i;
	}
	
	public static boolean deleteallMesage(int uid,int days){
		Calendar cal = Calendar.getInstance();
		cal.add(Calendar.DATE,-days);
		int oyear=cal.get(Calendar.YEAR);
		int omonth=cal.get(Calendar.MONTH)+1;
		int oday=cal.get(Calendar.DATE);
		String year = oyear+"";
		String month = omonth+"";
		if(month.length()<2) 
		{
			month="0"+month;
		}
		String day = oday+"";
		if (day.length()<2) {
			day = "0"+day;
		}
		String cmdText = "DELETE FROM tablemessage WHERE uid="+uid+"  and DATE_FORMAT(createtime,'%Y%m%d') <= '"+year+month+day+"' ";
		Object obj=SqlHelperwrite.execute(cmdText);
		return (int)obj>0;
		
		
	}
}
